package com.kangaroo.myhome.utils;

import org.springframework.beans.factory.annotation.Value;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class RunDet {

    @Value("${analysis.path}")
    private static String analysisPath;

    public static String sendRequest(String urlParam,String requestType) {

        HttpURLConnection con = null;

        BufferedReader buffer = null;
        StringBuffer resultBuffer = null;

        try {
            URL url = new URL(urlParam);
            //得到连接对象
            con = (HttpURLConnection) url.openConnection();
            //设置请求类型
            con.setRequestMethod(requestType);
            //设置请求需要返回的数据类型和字符集类型
            con.setRequestProperty("Content-Type", "application/json;charset=GBK");
            //允许写出
            con.setDoOutput(true);
            //允许读入
            con.setDoInput(true);
            //不使用缓存
            con.setUseCaches(false);
            //得到响应码
            int responseCode = con.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                //得到响应流
                InputStream inputStream = con.getInputStream();
                //将响应流转换成字符串
                resultBuffer = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, "GBK"));
                while ((line = buffer.readLine()) != null) {
                    resultBuffer.append(line);
                }
                return resultBuffer.toString();
            }

        }catch(Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void analysis_img(String in_src,String out_src)
    {
         String imgURL =analysisPath + "img?in_src="+in_src+"&out_src="+out_src;
         sendRequest(imgURL,"GET");
    }
    public static void analysis_video(String in_src,String out_src)
    {
        String imgURL =analysisPath + "video?in_src="+in_src+"&out_src="+out_src;
        sendRequest(imgURL,"GET");
    }
    public static void main(String[] args) {

 
        System.out.println("img detection start...");
        analysis_img("/home/ubuntu/workspace/test.png","/home/ubuntu/workspace/test_result.png");
        System.out.println("img detection finished");

        System.out.println("video detection start...");
        analysis_video("/home/ubuntu/workspace/test.mov","/home/ubuntu/workspace/test_result.mov");
        System.out.println("video detection finished");
    }
}
